$(function () {
    htysdy_init();
});

function htysdy_init() {

    // 合同定义加载
    $("[name='htysdy_htdy']").htdycombobox(false,'');

    var options = gethtysdyGridOption();
    $('#htysdy_grid').datagrid(options);
}

function gethtysdyGridOption() {

    var columns = [
        [
            {field: 'htdy_mc', title: '合同定义名称', width: 30},
            {field: 'mc', title: '合同元素名称', width: 30},
            {field: 'ysid', title: '元素id', width: 30},
            {field: 'dybzd', title: '对应表字段', width: 20},
            {field: 'yxlx', title: '选项类型', width: 30},
            {field: 'qz', title: '取值', width: 40},
            {field: 'mrz', title: '默认值', width: 40},
            {field: 'wbcd', title: '文本长度', width: 20},
            {field: 'isread', title: '是否可以修改', width: 20},
            {field: 'bz', title: '备注', width: 40}
        ]
    ];

    var options = {
        url: $.webPath() + "/htdygl/htysdy/htysdyList",
        toolbar: "#htysdy_toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        columns: columns,
        rownumbers: true,
        onClickRow: function (rowIndex, rowData) {
            if (rowData != null && rowData.id != null) {
                $("#htysdy_mod").show();
            }
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                // 转化类型
                if (this.yxlx == "1") {
                    this.yxlx = "文本";
                }else if (this.yxlx == "2"){
                    this.yxlx = "数据字典";
                }else if (this.yxlx == "3"){
                    this.yxlx = "数据集";
                }else if (this.yxlx == "4"){
                    this.yxlx = "日期";
                }else if (this.yxlx == "5"){
                    this.yxlx = "固定下拉";
                }else if(this.yxlx == "6"){
                    this.yxlx = "表格";
                }

                // 转化是否可以修改
                if(this.isread == "0"){
                    this.isread = "否";
                }else{
                    this.isread="是";
                }

            })
            return data;
        }
    };
    return options;
}

//*************按钮触发事件***********
//双击事件
$("#htysdy_grid").datagrid({
    onDblClickCell:function (index, field, value) {
        var record = $("#htysdy_grid").datagrid("getSelected");
        if (record == null) {
            $.messager.alert("提示", "请选择要更新的记录!", "info");
            return;
        }
        var href = $.webPath() + "/htdygl/htysdy/htysdy_content?htysdyId=" + record.id;
        var actionUrl = $.webPath() + "/htdygl/htysdy/modifyHtysdy?htysdyId=" + record.id;
        if (field == "htdy_mc") {
            showhtysdyDialog("修改合同元素定义", href, actionUrl);
        }
    }
});

//查询
function htysdy_search() {
    var htdyid = $("#htysdy_toolbar input[name='htysdy_htdy']").val();
    var htysmc = $("#htysdy_toolbar input[name='htysdy_mc']").val();
    var options = $.extend(gethtysdyGridOption(),
        {queryParams: {htdyId: htdyid, htysmc: htysmc}});
    $('#htysdy_grid').datagrid(options);
}
//修改元素定义
$("#htysdy_mod").click(function () {
    var record = $("#htysdy_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "info");
        return;
    }
    var href = $.webPath() + "/htdygl/htysdy/htysdy_content?htysdyId=" + record.id;
    var actionUrl = $.webPath() + "/htdygl/htysdy/modifyHtysdy?htysdyId=" + record.id;
    showhtysdyDialog("修改合同元素定义", href, actionUrl);
});

//弹出修改的窗口
function showhtysdyDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 600,
        height: 550,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '提交',
                handler: function () {
                    $.messager.progress();
                    $("#htysdyForm").form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close');
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $.messager.progress('close');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                loadhtysdy();
                                $.messager.show({
                                    title: '提示框',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//重新加载页面
function loadhtysdy() {
    var htdyid = $("#htysdy_toolbar input[name='htysdy_htdy']").val();
    var htysmc = $("#htysdy_toolbar input[name='htysdy_mc']").val();
    $('#htysdy_grid').datagrid('reload', {
        htdyId: htdyid,
        htysmc:htysmc
    });
}

